﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps;
using Microsoft.Phone.Controls.Maps.Core;
using FBEventMaker.GeoCodeService;

//GPS
using System.Device.Location;
//Dispacher
using System.Windows.Threading;
using System.ServiceModel;
using Microsoft.Phone.Controls.Maps.Platform;

namespace FBEventMaker
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Zoom Function in the Map
        private double zoom;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            zoom = 0;
        }

        private void buttonZoomIn_Click(object sender, RoutedEventArgs e)
        {
            double zoom = bingMap.ZoomLevel;
            bingMap.ZoomLevel = ++zoom;
        }
       
        private void buttonZoomOut_Click(object sender, RoutedEventArgs e)
        {
            double zoom = bingMap.ZoomLevel;
            bingMap.ZoomLevel = --zoom;
        }

        private void buttonFindMe_Click(object sender, RoutedEventArgs e)
        {
            // starting the GPS
            GpsServices gpsService = new GpsServices();

            gpsService.GpsServiceCompleted += (gpsServiceObj, gpsServiceArg) =>
            {
                resetPushPin(gpsServiceArg.Location, gpsServiceArg.Address);
            };

            gpsService.GetCurrentLocationAddress();
        }

        private void longitude_GotFocus(object sender, RoutedEventArgs e)
        {
            longitudeText.SelectAll();
        }

        private void latitude_GotFocus(object sender, RoutedEventArgs e)
        {
            latitudeText.SelectAll();
        }

        private void findThis_Click(object sender, RoutedEventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                double longitude;
                double latitude;

                if(!double.TryParse(longitudeText.Text, out longitude))
                {
                    return;
                }

                if (!double.TryParse(latitudeText.Text, out latitude))
                {
                    return;
                }

                if (longitude >= -180 && longitude <= 180 && latitude >= -90 && latitude <= 90)
                {
                    GpsServices gpsService = new GpsServices();
                    
                    gpsService.GpsServiceCompleted += (gpsServiceObj, gpsServiceArg) =>
                        {
                            resetPushPin(gpsServiceArg.Location, gpsServiceArg.Address);
                        };

                    gpsService.GetLocationAddress(new GeoCoordinate(latitude, longitude));
                }
                else
                {
                    MessageBox.Show("Enter between -180 and 180 for Longitude and between -90 and 90 for Latitude"); 
                }
            });
        }

        private void resetPushPin(GeoCoordinate location, string address)
        {
            Pushpin pin = new Pushpin();
            pin.Location = location;
           
            zoom = 14;
            if (string.IsNullOrEmpty(address))
            {
                MessageBox.Show("Name of the place not exist");
            }
            else
            {
                pin.Content = address;
            }

            bingMap.Children.Clear();
            bingMap.Children.Add(pin);
            bingMap.SetView(location, zoom);
        }

        private void faceBookButton_Click(object sender, RoutedEventArgs e)
        {
            // check the existance of the page
            if (!String.IsNullOrWhiteSpace("/Facebook/FacebookLogin.xaml"))
            {
                NavigationService.Navigate(new Uri("/Facebook/FacebookLogin.xaml", UriKind.Relative));
            }
        }

        private void searchTwitter_Click(object sender, RoutedEventArgs e)
        {
            // check the existance of the page
            if (!String.IsNullOrWhiteSpace("/Twitter/TwitterInfo.xaml"))
            {

                NavigationService.Navigate(new Uri("/Twitter/TwitterInfo.xaml", UriKind.Relative));
            }
        }

        private void m_RoadMode_Click(object sender, RoutedEventArgs e)
        {
            bingMap.Mode = new RoadMode();
        }

        private void m_AerialMode_Click(object sender, RoutedEventArgs e)
        {
            bingMap.Mode = new AerialMode();
        }
    }
}